﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using NUnit.Framework;
using ScrappyDB;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.IntegrationTests
{
    [TestFixture]
    public class BasicLinqTest
    {
        [TestFixtureSetUp]
        public  void Init()
        {
            var db = new Db();
            var newFoo = new Foo3
            {
                ID = 1,
                Bar = "phat"
            };
            db.SaveChanges(newFoo);

            var newFoo2 = new Foo3
            {
                ID = 2,
                Bar = "blah"
            };
            db.SaveChanges(newFoo2);

            var newFoo3 = new Foo3
            {
                ID = 3,
                Bar = "the quick brown fox"
            };
            db.SaveChanges(newFoo3);
        }

        [TestFixtureTearDown]
        public  void MyClassCleanup()
        {
            var db = new Db();
            var foos = db.Query<Foo3>();
            db.Delete<Foo3>(foos);
        }

        [Test]
        public void EmptyContextTest()
        {
            var context = new Foo3Context();
            var x =  context.Foos.ToList();

            Assert.AreEqual(3,x.Count);
        }

        [Test]
        public void BasicLinqItemNameTest()
        {
            var context = new Foo3Context();
            var x = from a in context.Foos where a.ID == 3 select a;

            Assert.AreEqual(1, x.ToList().Count());
        }

        [Test]
        public void BasicLinqPropertyTest()
        {
            var context = new Foo3Context();
            var x = from a in context.Foos where a.Bar == "blah" select a;

            Assert.AreEqual(1, x.ToList().Count());
            Assert.AreEqual(2, x.ToList().First().ID);
        }

        [Test]
        public void BasicLinqAndTest()
        {
            var context = new Foo3Context();
            var x = from a in context.Foos where a.Bar == "blah" & a.ID == 2 select a;

            Assert.AreEqual(1, x.ToList().Count());
        }

        [Test]
        public void BasicLinqOrTest()
        {
            var context = new Foo3Context();
            var x = from a in context.Foos where a.Bar == "blah" | a.ID == 1 select a;

            Assert.AreEqual(2, x.ToList().Count());
        }

        [Test]
        public void BasicLinqContainsTest()
        {
            var context = new Foo3Context();
            var x = from a in context.Foos where a.Bar.Contains("quick") select a;

            Assert.AreEqual(1, x.ToList().Count());
            Assert.AreEqual(3, x.ToList().First().ID);
        }
    }
    public class Foo3 
    {
        [Key]
        public int ID { get; set; }
        public string Bar { get; set; }
    }

    public class Foo3Context : ScrappyDB.Linq.SdbContext
    {
        public ScrappyDB.Linq.SdbSet<Foo3> Foos { get; set; }
    }
}
